%extra bus in midden van de lijn -> elke lijn in 2 verdelen
% clear all
%te bestuderen netwerk inladen:
load gekozen_netwerk;
eval(network_name);
%IEEE_14_bus_original;
extra_bus = size(Bus.con,1)+1;
extra_lijn_1 = size(Line.con,1)+1;
extra_lijn_2 = size(Line.con,1)+2;
for k = 1:size(Line.con,1)
    
        eval(network_name);
        Bus.con(extra_bus,:) = [extra_bus Line.con(k,4)  1.00000  0.00000  1  1];
        if exist('Line_copy_invoegen','var')==1
            if isempty(Line_copy_invoegen)~=1
            Line.con = Line_copy_invoegen;
            end
        end
        Line.con(k,16) = 0;
        Line.con(extra_lijn_1,:) = Line.con(k,:);
        Line.con(extra_lijn_2,:) = Line.con(k,:);
        
        %SIMULINK:
        
        Line.con(extra_lijn_1,1) = Line.con(k,1);
        Line.con(extra_lijn_1,2) = extra_bus;
        Line.con(extra_lijn_1,8) = Line.con(extra_lijn_1,8)/2;
        %blijkbaar niet /2 want dan singulariteiten op sommige lijnen
        Line.con(extra_lijn_1,9) = Line.con(extra_lijn_1,9);
        Line.con(extra_lijn_1,10) = Line.con(extra_lijn_1,10)*2;
        Line.con(extra_lijn_1,16) = 1;
        
        Line.con(extra_lijn_2,1) = extra_bus;
        Line.con(extra_lijn_2,2) = Line.con(k,2);
        Line.con(extra_lijn_2,8) = Line.con(extra_lijn_2,8)/2;
        %blijkbaar niet /2 want dan singulariteiten op sommige lijnen
        Line.con(extra_lijn_2,9) = Line.con(extra_lijn_2,9);
        Line.con(extra_lijn_2,10) = Line.con(extra_lijn_2,10)*2;
        Line.con(extra_lijn_2,16) = 1;  
        
        if Line.con(k,7)~=0
            Line.con(extra_lijn_1,7) = 0;
        end
        
        if exist('PV_copy_2','var')==1
            PV = PV_copy_2;
        end
        
        
        
        filename = sprintf('bus%03d.m',k);
        fid=fopen(filename,'wt');
        
        %fout aanleggen
        Fault.con = [ ... 
  extra_bus  100  Bus.con(extra_bus,2)  60  0.1  0.35  0.001  0.001;
 ];


        %print Bus.con
        count = fprintf(fid, 'Bus.con = [ ...\n');
        format =['%4d %8.4g %8.5g %8.5g %4u %4u;\n'];
        count = fprintf(fid,format,Bus.con');
        count = fprintf(fid,'   ];\n\n');
        %print SW.con
        count = fprintf(fid, 'SW.con = [ ...\n');
        format = ['%4d ',repmat('%8.4g ',1,10),'%2u %2u \n'];
        count = fprintf(fid,format,SW.con);
        count = fprintf(fid,'   ];\n\n');
        %print PV.con
        count = fprintf(fid, 'PV.con = [ ...\n');
        format = ['%4d ',repmat('%8.4g ',1,9),'%2u;\n'];
        count = fprintf(fid,format,PV.con');
        count = fprintf(fid,'   ];\n\n');
        %print PQ.con
        count = fprintf(fid, 'PQ.con = [ ...\n');
        format = ['%4d ',repmat('%8.4g ',1,6),'%2u %2u;\n'];
        count = fprintf(fid,format,PQ.con');
        count = fprintf(fid,'   ];\n\n');
%         %print Fault.con
% %         count = fprintf(fid, 'Fault.con = [ ...\n');
% %         format =['%4d ',repmat('%8.4g ',1,9),'%2u;\n'];
%         format =['%4d %8.4g %8.4g %8.4g %8.4g %8.4g %8.4g %8.4g'];
%         data = [1,100,400,60,0,0.25,0.001,0.001];
%         count = fprintf(fid, ['Fault.con = [',format,'];\n\n\n'],data);
% %         count = fprintf(fid,format,Fault.con');
% %         count = fprintf(fid,'   ];\n\n');
        %print Line.con
        count = fprintf(fid, 'Line.con = [ ...\n');
        format = ['%4d %4d ',repmat('%8.4g ',1,13),'%2u;\n'];
        count = fprintf(fid,format,Line.con');
        count = fprintf(fid,'   ];\n\n');
        
        
        %print Fault.con
        count = fprintf(fid, 'Fault.con = [... \n');
        count = fprintf(fid,'%4d %4.4g %4.4g %3.4g %2.4g %4.5g %6.5g %6.5g\n',Fault.con);
%         count = fprintf(fid,'\n');
        count = fprintf(fid,'   ];\n\n');
%         
        
        
%         Syn.con = [ ... 
%   1  615  69  60  5.2  0.2396  0  0.8979  0.6  0.23  7.4  0.03  0.646  0.646  0.4  0  0.033  10.296  2  0  0  1  1  0  0  0;
%   3  60  69  60  6  0  0.0031  1.05  0.185  0.13  6.1  0.04  0.98  0.36  0.13  0.3  0.099  13.08  2  0  0  1  1  0  0  0;
%   2  60  69  60  6  0  0.0031  1.05  0.185  0.13  6.1  0.04  0.98  0.36  0.13  0.3  0.099  13.08  2  0  0  1  1  0  0  0;
%   8  25  18  60  6  0.134  0.0014  1.25  0.232  0.12  4.75  0.06  1.22  0.715  0.12  1.5  0.21  10.12  2  0  0  1  1  0  0  0;
%   6  25  13.8  60  6  0.134  0.0014  1.25  0.232  0.12  4.75  0.06  1.22  0.715  0.12  1.5  0.21  10.12  2  0  0  1  1  0  0  0;
%  ]';

        %print Syn.con
        count = fprintf(fid, 'Syn.con = [... \n');
        format = [repmat('%8.4g ',1,26),';\n'];
        count = fprintf(fid,format,Syn.con');
%         count = fprintf(fid,'\n');
        count = fprintf(fid,'   ];\n\n');

% Exc.con = [ ... 
%   1  2  7.32  0  200  0.02  0.002  1  0.01  0.2  0.001  0.0006  0.9;
%   3  2  4.38  0  20  0.02  0.001  1  0.01  1.98  0.001  0.0006  0.9;
%   2  2  4.38  0  20  0.02  0.001  1  0.01  1.98  0.001  0.0006  0.9;
%   4  2  6.81  1.395  20  0.02  0.001  1  0.01  0.7  0.001  0.0006  0.9;
%   5  2  6.81  1.395  20  0.02  0.001  1  0.01  0.7  0.001  0.0006  0.9;
%  ]';
        
        %print Exc.con
        count = fprintf(fid, 'Exc.con = [... \n');
        format = [repmat('%8.4g ',1,13),';\n'];
        count = fprintf(fid,format,Exc.con');
%         count = fprintf(fid,'\n');
        count = fprintf(fid,'   ];\n\n');
        
        Varname.idx = [...
   65;    66;    67;    68;    69;    70;    71;
   72;    73;    74;    75;    76;    77;    78;
   79; 
   ]';

        %print Varname.idx
        count = fprintf(fid, 'Varname.idx = [... \n');
        format = [repmat('%8.4g; ',1,7),'\n'];
        count = fprintf(fid,format,Varname.idx);
        count = fprintf(fid,'   ];\n\n');
        
        if exist('Statcom','var')==1
        count = fprintf(fid, 'Statcom.con = [... \n');
        format = [repmat('%8.4g ',1,size(Statcom.con,2)),';\n'];
        count = fprintf(fid,format,Statcom.con');
        count = fprintf(fid,'   ];\n\n');
        end
        
        
buslist = [... 
  'Bus 01'; 'Bus 02'; 'Bus 03'; 'Bus 04'; 'Bus 05'; 
  'Bus 06'; 'Bus 07'; 'Bus 08'; 'Bus 09'; 'Bus 10'; 
  'Bus 11'; 'Bus 12'; 'Bus 13'; 'Bus 14'; 'Bus 15'];
        fprintf(fid,'Bus.names = { ...\n');
        for i = 1:length(buslist(:,1))
          fprintf(fid,'    ''%s'';\n',buslist(i,:));
        end
        fprintf(fid,'   };\n\n');
        %print Areas.names
        Areasnames = [ ...
          'IEEE30BUS'];
        fprintf(fid,'Areas.names = { ...\n');
        for i = 1:length(Areasnames(:,1))
          fprintf(fid,'    ''%s''\n',Areasnames(i,:));
        end
        fprintf(fid,'   };\n\n');
        

        fclose(fid);
    
end